clear
clear mata
clear matrix

cd ..                                                                            /* Goes back to parent folder */

capture log close
log using "log/08_Droughts.log", replace

*       Patriotism!

*       FIRST VERSION  June        7, 2022
*       THIS VERSION   June       13, 2022
*       LAST RUN       June       13, 2022

*       LAST REVISOR		BC

*       Log of revisions: 
*							AT (09.06.2022) - included CLIMDIVX
*							AT (13.06.2022) - included part B for ASN

*       This prepares drought data

********************************************************************************
****                      PLAN OF THE PROCEDURE                             ****
****  					A. County-level dataset                 	        ****
****  1. Prepare drought data                                               ****
****  2. Reshape database to have 1 observation per climatic division       ****
****  3. Merge data to correspondence b/w climatic divisions and counties   ****
****  4. Collapse at county level                                           ****
****  5. Create variables of interest                                       ****
****  6. Label and save                                                     ****
****  					B. ASN dataset                                 	    ****
****  1. Prepare drought data                                               ****
****  2. Reshape database to have 1 observation per climatic division       ****
****  3. Merge data to correspondence b/w climatic divisions and counties   ****
****  4. Collapse at county level                                           ****
****  5. Create variables of interest                                       ****
****  6. Label and save                                                     ****
********************************************************************************
****                                                                        ****
********************************************************************************

********************************************************************************
****  					A. County-level dataset                         	****
********************************************************************************

********************************************************************************
****  1. Prepare drought data                                               ****
********************************************************************************
do "code-build/01_Shopping_List.do"

import delimited "rawdata/NOAA/climdiv-pdsidv-v1.0.0-20170404.txt", delimiter(space, collapse) stringcols(1) clear 

gen      year    = substr(v1, -4, .)
gen      CLIMDIV = substr(v1,  1, 4)
destring year CLIMDIV, replace

keeporder CLIMDIV year v2-v13                                                     /* v14 is always empty */

********************************************************************************
****  2. Reshape database to have 1 observation per climatic division       ****
********************************************************************************
reshape long     v, i(CLIMDIV year) j(month)
ren              v psdi
replace            psdi = . if psdi < -20                                         /* Palmer Drought Index format (f7.2): Range of values -20.00 to 20.00. */
gen       SUM3MO_DROUGHT = cond(psdi <= -3,1,0) if !missing(psdi) & month >= 7 & month <=  9 
                                                                                  /* v2 = JAN, v13 = DEC: summer precipitation=Jun-Aug */

collapse (sum ) SUM3MO_DROUGHT, by(year CLIMDIV)
drop     if year < 1897 | year > 1940                                             /* Not interested in these */
reshape   wide  SUM3MO_DROUGHT, i(CLIMDIV) j(year)

********************************************************************************
****  3. Merge data to correspondence b/w climatic divisions and counties   ****
********************************************************************************
merge 1:m CLIMDIV using "data/Bridge-CLIMDIV-countynd", nogen keep(mat)          /* Not matched are 32 climatic divisions: 13 from AK (50XX) 6 from HI (51XX), 6 from PR (66XX), 3 from Virgin Islands (67XX), 4 from Pacific Islands (91XX) */
for VAR in var SUM3MO_DROUGHT* : replace VAR = VAR * weightCD

egen xweightCD = max(weightCD), by(stateicpsr countynd)
 gen CLIMDIVX  =     CLIMDIV if weightCD == xweightCD
 
********************************************************************************
****  4. Collapse at county level                                           ****
********************************************************************************
collapse (min) CLIMDIVX (sum) SUM3MO_DROUGHT*, by(stateicpsr countynd)


********************************************************************************
****  5. Label and save                                                     ****
********************************************************************************
lab var stateicpsr           "State code (ICPSR)"
lab var countynd             "County code (Fishback et al. 2003)"
lab var CLIMDIVX             "Climatic division with greatest overlap with county (NOAA)"    

for NUM in num 1897/1940 : lab var SUM3MO_DROUGHTNUM  "Number of summer (Jun-Aug) months with PSDI < -3 in NUM (NOAA)"

save "data/droughts", replace

********************************************************************************
****  					B. ASN dataset                        	            ****
********************************************************************************

********************************************************************************
****  1. Prepare drought data                                               ****
********************************************************************************

import delimited "rawdata/NOAA/climdiv-pdsidv-v1.0.0-20170404.txt", delimiter(space, collapse) stringcols(1) clear 

gen      year    = substr(v1, -4, .)
gen      CLIMDIV = substr(v1,  1, 4)
destring year CLIMDIV, replace

keeporder CLIMDIV year v2-v13                                                     /* v14 is always empty */

********************************************************************************
****  2. Reshape database to have 1 observation per climatic division       ****
********************************************************************************
reshape long     v, i(CLIMDIV year) j(month)
ren              v psdi
replace            psdi = . if psdi < -20                                         /* Palmer Drought Index format (f7.2): Range of values -20.00 to 20.00. */
gen       SUM3MO_DROUGHT = cond(psdi <= -3,1,0) if !missing(psdi) & month >= 7 & month <=  9 
                                                                                  /* v2 = JAN, v13 = DEC: summer precipitation=Jun-Aug */

collapse (sum ) SUM3MO_DROUGHT, by(year CLIMDIV)
drop     if year < 1897 | year > 1940                                             /* Not interested in these */
reshape   wide  SUM3MO_DROUGHT, i(CLIMDIV) j(year)

gen  SUM3MO_DROUGHT3340 = SUM3MO_DROUGHT1933 + SUM3MO_DROUGHT1934 + SUM3MO_DROUGHT1935 + SUM3MO_DROUGHT1936 + SUM3MO_DROUGHT1937 + SUM3MO_DROUGHT1938 + SUM3MO_DROUGHT1939 + SUM3MO_DROUGHT1940

********************************************************************************
****  3. Merge data to correspondence b/w climatic divisions and counties   ****
********************************************************************************
merge 1:m CLIMDIV using "data/Bridge-CLIMDIV-county1940", nogen keep(mat)          /* Not matched are 32 climatic divisions: 13 from AK (50XX) 6 from HI (51XX), 6 from PR (66XX), 3 from Virgin Islands (67XX), 4 from Pacific Islands (91XX) */
for VAR in var SUM3MO_DROUGHT* : replace VAR = VAR * weightCD

egen xweightCD = max(weightCD), by(stateicpsr countyicpsr40)
 gen CLIMDIVX  =     CLIMDIV if weightCD == xweightCD
 
replace countyicpsr40 =  605 if stateicpsr == 72 & countyicpsr40 ==  610 /* Union, Oregon.  */
replace countyicpsr40 =    5 if stateicpsr == 65 & countyicpsr40 ==  250 /* Carson, Nevada is really Ormsby, Nevada.  */
replace countyicpsr40 =  250 if stateicpsr == 65 & countyicpsr40 ==  270 /* Pershing, Nevada.  */

 
********************************************************************************
****  4. Collapse at county level                                           ****
********************************************************************************
collapse (min) CLIMDIVX (sum) SUM3MO_DROUGHT3340, by(stateicpsr countyicpsr40)


********************************************************************************
****  5. Label and save                                                     ****
********************************************************************************
lab var stateicpsr           "State code (ICPSR)"
lab var countyicpsr40        "County code (IPUMS)"
lab var CLIMDIVX             "Climatic division with greatest overlap with county (NOAA)"    
lab var SUM3MO_DROUGHT3340   "Number of summer (Jun-Aug) months with PSDI < -3 in 1933-40 (NOAA)"

save "data/droughts_ASN", replace

log close
exit
